import javax.swing.*;

/**
 * Created by L.jp
 * Description:
 * User: 86189
 * Date: 2023-04-24
 * Time: 20:10
 */
class TreeNode {
    int val = 0;
   TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
      this.val = val;
    }
  }
public class Solution {
    public boolean isBalanced (TreeNode node) {
        if(node==null){
            return true;
        }
        //计算左右子树高度
        int diff=Math.abs(getHeight(node.left)-getHeight(node.right));
        if(diff>1){
            return false;
        }
        return isBalanced(node.left) && isBalanced(node.right);
    }
    
    private int getHeight(TreeNode root) {
        if(root==null){
            return 0;
        }
        int leftHeight=getHeight(root.left);
        int rightHeight=getHeight(root.right);
        return Math.max(leftHeight, rightHeight)+1;
    }
}
